<template>
  <div id="app">
    <!-- 登录页面 -->
    <div v-if="!isLogin">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="form.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toLogin('ruleForm')"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
      <!-- <button @click="isLogin=true">跳转</button> -->
    </div>
    <div v-else>
      <el-container>
        <el-header> </el-header>
        <el-container>
          <el-aside width="200px">
            <!-- 在el-menu上写了router之后启动路由，内部的index属性代表导航打开的路由，注意路由对应的页面是在src/router/index.js中配置的 -->
            <el-menu
              :default-active="active"
              class="el-menu-vertical-demo"
              @open="handleOpen"
              @close="handleClose"
              router
            >
              <el-menu-item index="/">
                <i class="el-icon-menu"></i>
                <span slot="title">疫情动态</span>
              </el-menu-item>
              <el-menu-item index="/map">
                <i class="el-icon-menu"></i>
                <span slot="title">疫情地图</span>
              </el-menu-item>
              <el-menu-item index="/current">
                <i class="el-icon-menu"></i>
                <span slot="title">实时疫情</span>
              </el-menu-item>
              <el-menu-item index="/count">
                <i class="el-icon-menu"></i>
                <span slot="title">数据统计</span>
              </el-menu-item>
              <el-menu-item index="/hosp">
                <i class="el-icon-menu"></i>
                <span slot="title">定点医院</span>
              </el-menu-item>
              <el-submenu index="2">
                <template slot="title">
                  <i class="el-icon-location"></i>
                  <span>疫情管理</span>
                </template>
                <el-menu-item index="/hospital">定点医院</el-menu-item>
                <el-menu-item index="/infoList">资讯分类</el-menu-item>
                <el-menu-item index="/article">新闻资讯</el-menu-item>
              </el-submenu>

              <el-menu-item index="/up">
                <i class="el-icon-setting"></i>
                <span slot="title">数据上报</span>
              </el-menu-item>

              <el-submenu index="3">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>疫情管理</span>
                </template>
                <el-menu-item index="/list">数据列表</el-menu-item>
                <el-menu-item index="/up">数据上报</el-menu-item>
              </el-submenu>
            </el-menu>
          </el-aside>
          <el-main>
            <!-- 路由对应的组件展示的位置 -->
            <div>
              <router-view></router-view>
            </div>
          </el-main>
        </el-container>
      </el-container>
    </div>
  </div>
</template>

<script>
// vuex  状态机  处理组件公用的数据
// h5  sessionStorage 在登录页面点击登录按钮的时候，存token数据到sessionStorage，其他页面如果要用token数据，从sessionStorage里取
import axios from "./utils/request";
export default {
  name: "app",
  data() {
    return {
      active: "/",
      isLogin: sessionStorage.getItem("token"),
      form: {},
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  created() {
    this.active = location.pathname;
    // console.log(location.pathname, "=====");
    // console.log(this.$route, "000");
  },
  methods: {
    //登录
    toLogin(formName) {
      this.$refs[formName].validate(async (valid) => {
        // 校验通过
        if (valid) {
          let res = await axios.post("/user/login", this.form);
          if (res.status == 200) {
            // 存token到sessionStorage中
            sessionStorage.setItem("token", res.data.token);
            this.isLogin = true;
          } else {
            //提示登录失败的信息
            this.$notify.error({
              title: "错误",
              message: res.statusText,
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
  },
};
</script>

<style>
body {
  margin: 0;
}
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
}
.el-header {
  background-color: #b3c0d1;
  color: #333;
  line-height: 60px;
}

.el-aside {
  /* background-color: #d3dce6; */
  color: #333;
  /* line-height: 200px; */
  height: calc(100vh - 60px);
  min-height: 500px;
}

.el-main {
  /* background-color: #e6f2f2; */
  background-color: rgba(0, 128, 128, 0.1);
  color: #333;
  /* line-height: 160px; */
  height: calc(100vh - 60px);
  min-height: 500px;
  padding: 10px !important;
}
.el-main > div {
  background-color: #fff;
  border-radius: 10px;
  min-height: calc(100% - 40px);
  padding: 20px;
}
</style>
